<?php
  /**
   * Kontrollbase
   *
   * An open source MySQL monitoring and analytics application
   *
   * @package Kontrollbase
   * @author Matt Reid
   * @copyright Copyright (c) 2009 Matt Reid, Kontrollbase LLC
   * @license http://kontrollsoft.com/kontrollbase/userguide/general-LICENSE.php
   * @link http://kontrollbase.com
   */

class Hosts extends Controller {

  function Hosts()
  {
    parent::Controller();
  }

  function index() {  
    $this->load->model('Model_hosts', 'hosts');
    log_message('debug', "hosts_index function called");
   
    $g['data'] = $this->hosts->get_data();
    $g['root'] = $this->config->item('base_url');
    $g['lastID'] = $this->hosts->get_lastid();
    
    $this->load->view('main/hosts',$g);
  }  

  function jsonstore() {
    $this->load->model('Model_hosts', 'hosts');
    log_message('debug', "hosts_jsonstore function called");
    $data = $this->hosts->get_data();

    $i = 1;
    $r=0;
    $u=count($data);
    print "{
            \"count\":$u,
            \"hosts\":[\n";
           
    foreach($data as $key => $value) {
      foreach($value as $k => $v) {
	if($k == 'id') { $data_id=$v;}
	$$k = $v;
      }

      print "{\"host_list_id\":\"$id\",
\"host_list_hostname':'$host_list_hostname\",
\"host_list_ipaddress':'$host_list_ipaddress\",
\"host_list_uuid':'$host_list_uuid\",
\"template_list_name':'$template_list_name\"
}
";
      $r++;
      if($r<$u) { print ",";}
      else {
	print "]}";
      }
    }
  }

  function submit() {
    log_message('debug', "hosts_submit function called");
    $this->load->model('Model_hosts', 'hosts');
    $this->load->library('form_validation');  

    $action = $this->input->post("action");
    log_message('debug', "hosts_submit: action = $action");

    if($action == "update") {
      log_message('debug', "hosts_submit: action = update");

      $field = $this->input->post("field");
      $value = $this->input->post("value");
      $id = $this->input->post("id");

      if($field == "host_list_uuid") {
        log_message('debug', "Not altering $field to lowercase.");
      }
      else {
        $value = strtolower($value);
        log_message('debug', "Changing value of $field to lowercase.");
      }

      log_message('debug', $_POST['id']);
      log_message('debug', "hosts_submit: id: $id, field: $field, value: $value");
      
      $state = $this->hosts->update_data($id,$field,$value);
      if($state == 0) {
        log_message('debug', "controller: JSON = {success: true}");
        echo "{success: true}";
      }
      elseif($state == 1) {
        log_message('debug', "failed: JSON = success: false, errors: { reason: 'Add failed. Please retry.' }}");
        echo "{success: false, errors: { reason: 'Add failed. Please retry.' }}";
      }
      else {
        show_error("This is a general failure message.");
      }
    }

    if($action == "insert") {
      log_message('debug', "hosts_submit: action = insert");
      $UUID = ($this->uri->segment(3))?$this->uri->segment(3):0;
      $state = $this->hosts->add_data($UUID);
      if($state == 0) {
        log_message('debug', "controller: JSON = {success: true}");
        echo "{success: true}";
      }
      elseif($state == 1) {
        log_message('debug', "failed: JSON = success: false, errors: { reason: 'Insert failed. Please retry.' }}");
        echo "{success: false, errors: { reason: 'Add failed. Please retry.' }}";
      }
      else {
        show_error("This is a general failure message.");
      }
    }

    if($action == "delete") {
      log_message('debug', "hosts_submit: action = delete");

      $this->form_validation->set_rules("id", "id", 'trim|required');
      $id = $this->input->post("id");
      log_message('debug', "hosts_submit: delete2: id: $id");
      
      if ($this->form_validation->run() == FALSE) {    
        log_message('debug', "submit controller: form validation failed.");
        echo "{success: false, errors: { reason: 'Delete::Form validation failed. Please retry.'}}";
      }
      else {
        $state = $this->hosts->delete_data($id);
        if($state == 0) {
          log_message('debug', "Submit controller: JSON = {success: true}");
          echo "{success: true}";
        }
        elseif($state == 1) {
          log_message('debug', "Submit failed: JSON = success: false, errors: { reason: 'Delete failed. Please retry.' }}");
          echo "{success: false, errors: { reason: 'Delete failed. Please retry.' }}";
        }
        else {
          show_error("This is a general failure message.");
        }
      }
    }

  }
}


?>